package com.upload.service;

import com.github.sd4324530.fastweixin.api.OauthAPI;
import com.github.sd4324530.fastweixin.api.response.GetUserInfoResponse;
import com.github.sd4324530.fastweixin.api.response.OauthGetTokenResponse;
import com.upload.bean.yCoin.LoginResponseBean;
import com.upload.util.CookieUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by asus on 2017/3/24.
 */
@Service
public class UserInfoService {

    @Autowired
    private RedisTemplate<String,LoginResponseBean> redisTemplate;

    @Autowired
    private OauthAPI oauthAPI;

    public boolean handleLogin(HttpServletRequest request){
        return new CookieUtil().getCookie(request,"cookieToken",redisTemplate);
    }

    public GetUserInfoResponse getUserInfo(String code){
        //获取Token
        OauthGetTokenResponse oauthGetTokenResponse = oauthAPI.getToken(code);
        //获取用户信息
        GetUserInfoResponse getUserInfoResponse = oauthAPI.
                getUserInfo(oauthGetTokenResponse.getAccessToken(), oauthGetTokenResponse.getOpenid());
        System.out.println(getUserInfoResponse.getNickname());
        System.out.println(getUserInfoResponse.getOpenid());
        return getUserInfoResponse;
    }
}
